import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { StoreWarehouseLocation } from "@/api/backend/index";
import { handleTree } from "@/utils/common";

export const useStoreWarehouseLocation = defineStore(
  Names.StoreWarehouseLocation,
  {
    state: () => {
      return {
        type: [],
      };
    },
    getters: {},
    actions: {
      //获取库位信息
      getLocation() {
        return new Promise((resolve, reject) => {
          _get(StoreWarehouseLocation + "/list", {}, "json", "")
            .then((res) => {
              console.log("获取库位信息", res);
              resolve(res);
            })
            .catch((err) => {
              console.log(err);
              reject(err);
            });
        });
      },
      //获取库位下拉树结构
      // getLocationTree() {
      //   return new Promise((resolve, reject) => {
      //     _get(StoreWarehouseLocation + "/treeselect", {}, "json", "")
      //       .then((res) => {
      //         console.log("获取库位下拉树结构", res);
      //         resolve(res);
      //       })
      //       .catch((err) => {
      //         console.log(err);
      //         reject(err);
      //       });
      //   });
      // },
      getLocationTree() {
        return new Promise((resolve, reject) => {
          _get(StoreWarehouseLocation + "/list", {}, "json", "")
            .then((res: any) => {
              let data = handleTree(res.rows, "memoryId", "", "");
              console.log("获取库位下拉树结构", res, data);
              resolve(data);
            })
            .catch((err) => {
              console.log(err);
              reject(err);
            });
        });
      },
    },
  }
);
